<?php 
$guestbook = new DomDocument(); //创建一个新的 DOM对象 

$guestbook->load('DB/guestbook.xml'); //读取 XML数据 
$threads = $guestbook->documentElement; //获得 XML结构的根 
//创建一个新 thread节点 
$thread = $guestbook->createElement('thread'); 
$threads->appendChild($thread); 
//在新的 thread节点上创建 id标签 
$id = $guestbook->createElement('id'); 
$id->appendChild($guestbook->createTextNode(rand(1,10000))); 
$thread->appendChild($id); 
//在新的 thread节点上创建 title标签 
$title = $guestbook->createElement('title'); 
$title->appendChild($guestbook->createTextNode($_POST['title'])); 
$thread->appendChild($title); 
//在新的 thread节点上创建 author标签 
$author = $guestbook->createElement('author'); 
$author->appendChild($guestbook->createTextNode($_POST['author'])); 
$thread->appendChild($author); 
//在新的 thread节点上创建 content标签 
$content = $guestbook->createElement('content'); 
$content->appendChild($guestbook->createTextNode($_POST['content'])); 
$thread->appendChild($content); 

//将 XML数据写入文件 
$fp = fopen("DB/guestbook.xml", "w"); 
if(fwrite($fp, $guestbook->saveXML())) {
	echo "<meta charset=utf-8>";
	echo "留言提交成功"; 
	header('Location: ./index.php');
}
else {
	echo "<meta charset=utf-8>";
	echo "留言提交失败"; 
	header('Location: ./index.php');
}
fclose($fp); 
?> 
